package cn.edu.scnu.mapper;

import cn.edu.scnu.entity.Movie;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface MovieMapper extends BaseMapper<Movie> {
    @Select("SELECT * FROM movies WHERE actors LIKE CONCAT('%', #{actorName}, '%')")
    List<Movie> findMoviesByActorName(String actorName);

    @Select("<script>" +
            "SELECT * FROM movies " +
            "WHERE 1=1 " +
            "<if test='type != null and type != \"\"'>AND type LIKE CONCAT('%', #{type}, '%')</if>" +
            "<if test='region != null and region != \"\"'>AND region LIKE CONCAT('%', #{region}, '%')</if>" +
            "LIMIT #{size} OFFSET #{offset}" +
            "</script>")
    List<Movie> findMovies(@Param("size") int size, @Param("offset") int offset,
                           @Param("type") String type, @Param("region") String region);

    @Select("<script>" +
            "SELECT COUNT(*) FROM movies " +
            "WHERE 1=1 " +
            "<if test='type != null and type != \"\"'>AND type LIKE CONCAT('%', #{type}, '%')</if>" +
            "<if test='region != null and region != \"\"'>AND region LIKE CONCAT('%', #{region}, '%')</if>" +
            "</script>")
    int countMovies(@Param("type") String type, @Param("region") String region);
}